﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Globalization;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Windows.Markup;
using DAL;

namespace SS.WPFusercontrol
{
    /// <summary>
    /// Interaction logic for Sale.xaml
    /// </summary>
    public partial class Sale : UserControl
    {
        public Sale()
        {
            InitializeComponent();
            LoadProductNames();
            LoadCustomerNames();
            SalesDatePicker.SelectedDate = DateTime.Today;
        }

        private void LoadProductNames()
        {
            Products products = new Products();
            DataTable DT=products.GetProductNames();
            
            ProductNamecomboBox.ItemsSource = DT.DefaultView;
            ProductNamecomboBox.DisplayMemberPath = DT.Columns["ProductName"].ToString();
            ProductNamecomboBox.SelectedValue = DT.Columns["ProductID"].ToString();
            //ProductNamecomboBox.Items.Insert(0,
        }

        private void LoadCustomerNames()
        {
            Customers customers = new Customers();
            DataTable DT = customers.GetCustomerNames();

            CustomerNameComboBox.ItemsSource = DT.DefaultView;
            CustomerNameComboBox.DisplayMemberPath = DT.Columns["CustomerName"].ToString();
            CustomerNameComboBox.SelectedValue = DT.Columns["CustomerID"].ToString();
            //ProductNamecomboBox.Items.Insert(0,
        }

        private bool IsValidate()
        {
            bool isValidate = false;

            if (ProductNamecomboBox.Text.Trim() == string.Empty || ProductNamecomboBox.Text.Trim() == "请选择产品")
            {
                ErrorLabel.Content = "产品名称不能为空，请选择！";
                ErrorLabel.Foreground = Brushes.Red;
            }
            else if (CustomerNameComboBox.Text.Trim() == string.Empty || CustomerNameComboBox.Text.Trim() == "请选择客户")
            {
                ErrorLabel.Content = "客户名称不能为空，请选择！";
                ErrorLabel.Foreground = Brushes.Red;
            }
            else
            {
                if (SalesNumberTextBox.Text.Trim() == string.Empty || UnitPriceTextBox.Text.Trim() == string.Empty || SalesTotalTextBox.Text.Trim() == string.Empty)
                {
                    ErrorLabel.Content = "产品销量/单价/总金额不能为空，请补充！";
                    ErrorLabel.Foreground = Brushes.Red;
                }
                else
                {
                    isValidate = true;
                }
            }            
            return isValidate;
        }

        private void QueryButton_Click(object sender, RoutedEventArgs e)
        {
            Sales sales = new Sales();
            ErrorLabel.Visibility = Visibility.Hidden;
            string productName = ProductNamecomboBox.Text.Trim() == "请选择产品" ? "" : ProductNamecomboBox.Text.Trim();
            string customerName = CustomerNameComboBox.Text.Trim() == "请选择客户" ? "" : CustomerNameComboBox.Text.Trim();
            DataTable DT = sales.GetSalesByProductName(productName, customerName);
            DT.DefaultView.AllowNew = false;
            dataGrid1.ItemsSource = DT.DefaultView;         
        }

        private void InsertButton_Click(object sender, RoutedEventArgs e)
        {
            if (IsValidate())
            {
                Sales sales = new Sales();
                if (sales.SaveSales(int.MinValue, (int)((System.Data.DataRowView)(ProductNamecomboBox.SelectedValue)).Row.ItemArray[0], DesriptionTextbox.Text.Trim(), SalesDatePicker.SelectedDate.Value, Int32.Parse(SalesNumberTextBox.Text.Trim()), float.Parse(SalesTotalTextBox.Text.Trim()), float.Parse(UnitPriceTextBox.Text.Trim())))
                {
                    ErrorLabel.Content = "添加成功！";
                    ErrorLabel.Foreground = Brushes.Green;
                    DataTable DT = sales.GetSalesByProductName(ProductNamecomboBox.Text.Trim(), CustomerNameComboBox.Text.Trim());
                    DT.DefaultView.AllowNew = false;
                    dataGrid1.ItemsSource = DT.DefaultView;
                }
                else
                {
                    ErrorLabel.Content = "添加失败，请重新操作！如仍然失败，请联系管理员！";
                    ErrorLabel.Foreground = Brushes.Red;
                }
            }
            ErrorLabel.Visibility = Visibility.Visible; 
        }

        private void SelectAllButton_Click(object sender, RoutedEventArgs e)
        {
            if (dataGrid1.Items.Count > 0)
            {
                CheckBox FirstCheckBoxInCell = dataGrid1.Columns[0].GetCellContent(dataGrid1.Items[0]) as CheckBox;
                bool IsCheckedCheckBox = (bool)FirstCheckBoxInCell.IsChecked;
                foreach (var item in dataGrid1.Items)
                {
                    CheckBox selectCheckBoxInCell = dataGrid1.Columns[0].GetCellContent(item) as CheckBox;
                    if (selectCheckBoxInCell != null)
                    {
                        selectCheckBoxInCell.IsChecked = !IsCheckedCheckBox;
                    }
                }

            }
        }

        private void DeleteButton_Click(object sender, RoutedEventArgs e)
        {
            string IDs = "";
            if (dataGrid1.Items.Count > 0)
            {
                foreach (DataRowView item in dataGrid1.Items)
                {
                    CheckBox selectCheckBoxInCell = dataGrid1.Columns[0].GetCellContent(item) as CheckBox;
                    if (selectCheckBoxInCell.IsChecked == true)
                    {
                        IDs = IDs + item["SalesID"].ToString() + ",";
                    }
                }
            }

            Sales sales = new Sales();
            if (sales.DeleteSales(IDs.TrimEnd(',')))
            {
                ErrorLabel.Content = "成功删掉！";
                ErrorLabel.Foreground = Brushes.Green;
                DataTable DT = sales.GetSalesByProductName(ProductNamecomboBox.Text.Trim(), CustomerNameComboBox.Text.Trim());
                DT.DefaultView.AllowNew = false;
                dataGrid1.ItemsSource = DT.DefaultView;
            }
            else
            {
                ErrorLabel.Content = "删掉失败，请重新操作！如仍然失败，请联系管理员！";
                ErrorLabel.Foreground = Brushes.Red;
            }
        }

        private void dataGrid1_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            object item = dataGrid1.CurrentItem;
            if (item != null)
            {
                ProductNamecomboBox.Text = ((DataRowView)(item)).Row.ItemArray[3].ToString();
                CustomerNameComboBox.Text = ((DataRowView)(item)).Row.ItemArray[4].ToString();
                DesriptionTextbox.Text = ((DataRowView)(item)).Row.ItemArray[8].ToString();
                SalesDatePicker.SelectedDate = (DateTime)((DataRowView)(item)).Row.ItemArray[1];
                SalesNumberTextBox.Text = ((DataRowView)(item)).Row.ItemArray[5].ToString();
                UnitPriceTextBox.Text = ((DataRowView)(item)).Row.ItemArray[6].ToString();
                SalesTotalTextBox.Text = ((DataRowView)(item)).Row.ItemArray[7].ToString();

                //SetEditable(false);
            }
        }

        private void SetEditable(bool IsEditable)
        {
            DesriptionTextbox.IsEnabled = IsEditable;
            SalesDatePicker.IsEnabled = IsEditable;
            SalesNumberTextBox.IsEnabled = IsEditable;
            UnitPriceTextBox.IsEnabled = IsEditable;
            SalesTotalTextBox.IsEnabled = IsEditable;
            InsertButton.IsEnabled = IsEditable;
        }
    }
}
